package com.chinairi.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.IdGenerator;
import com.chinairi.common.utils.RedisUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.*;
import com.chinairi.mall.dao.*;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.*;
import com.chinairi.mall.service.ICompShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业店铺表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-06
 */
@Service
public class CompShopServiceImpl extends ServiceImpl<CompShopMapper, CompShop> implements ICompShopService {

    @Autowired
    private CompShopMapper compShopMapper;
    @Autowired
    private UserCompanyInfoServiceImpl userCompanyInfoService;
    @Autowired
    private GoodsInfoServiceImpl goodsInfoService;
    @Autowired
    private ServiceShelfServiceImpl serviceShelfService;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private ServiceShelfMapper serviceShelfMapper;
    @Autowired
    private FourNewGoodsServiceImpl fourNewGoodsService;
    @Autowired
    private FourNewGoodsMapper fourNewGoodsMapper;
    @Autowired
    private OrderServicePurchaseMapper orderServicePurchaseMapper;
    @Autowired
    private OrderServiceQualityMapper orderServiceQualityMapper;
    @Autowired
    private OrderServiceTransportMapper orderServiceTransportMapper;
    @Autowired
    private OrderServiceStorageMapper orderServiceStorageMapper;
    @Autowired
    private OrderServiceSettleMapper orderServiceSettleMapper;
    @Autowired
    private OrderServicePurchaseGoodsMapper orderServicePurchaseGoodsMapper;
    @Autowired
    private OrderServiceQualityGoodsMapper orderServiceQualityGoodsMapper;
    @Autowired
    private OrderServiceTransportGoodsMapper orderServiceTransportGoodsMapper;
    @Autowired
    private OrderServiceSettleGoodsMapper orderServiceSettleGoodsMapper;
    @Autowired
    private OrderServiceStorageGoodsMapper orderServiceStorageGoodsMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private GoodsInfoApproveRecorderMapper goodsInfoApproveRecorderMapper;
    @Autowired
    private ServiceShelfApproveRecorderMapper serviceShelfApproveRecorderMapper;
    @Autowired
    private FourNewGoodsApproveRecorderMapper fourNewGoodsApproveRecorderMapper;

    /**
     * 新建店铺
     *
     * @param compShop 店铺信息
     */
    @Override
    public void add(CompShop compShop) {
        compShop.setCsDbid(UUID.randomUUID().toString().replace("-", ""));
        CompanyTypeMO result = userCompanyInfoService.getCom(null);
        if (result == null || "".equals(result.getUciDbid())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "您暂时没有企业信息，请先申请企业资质！");
        }
        compShop.setUciDbid(result.getUciDbid());
        compShopMapper.insert(compShop);
        userCompanyInfoService.putShopInfoToRedis();
    }

    /**
     * 更新店铺信息
     *
     * @param compShop 店铺信息
     */
    @Override
    public void updateShop(CompShop compShop) {
        compShopMapper.updateById(compShop);
        userCompanyInfoService.putShopInfoToRedis();
        orderInfoMapper.updateOrderShopName(compShop);
    }

    /**
     * 根据用户信息获取用户企业下的某个类别的店铺
     *
     * @param type 前端回传店铺类别
     * @return CompShop
     */
    @Override
    public CompShopVO getAllByType(String type) {
        CompShop compShop = compShopMapper.getAllByType(type, UserSession.getUserId());
        if (compShop == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "您暂时没有店铺，请先申请店铺！");
        }
        CompShopVO compShopVO = new CompShopVO();
        BeanUtils.copyProperties(compShop, compShopVO);
        BeanUtils.copyProperties(userCompanyInfoService.getShopInfoAndCompInfo(compShop.getCsDbid()), compShopVO);
        CompShopMO compShopMO = new CompShopMO();
        compShopMO.setCsDbid(compShop.getCsDbid());
        compShopMO.setCsShopType(type);
        compShopVO.setGoodsAndServiceVOList(getGoodsOrServiceByShopId(compShopMO));
        return compShopVO;
    }

    /**
     * 单独调用的方法
     *
     * @param compShopMo
     * @return
     */
    private List<GoodsAndServiceVO> getGoodsOrServiceByShopId(CompShopMO compShopMo) {
        List<GoodsAndServiceVO> goodsAndServiceVoList;
        if ("0".equals(compShopMo.getCsShopType())) {
            GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
            goodsInfoVO.setGoodsShelfState(2);
            goodsInfoVO.setGoodsApproveStatus(1);
            List<GoodsInfo> goodsInfoList = goodsInfoService.getListByCsDbId(goodsInfoVO, compShopMo.getCsDbid());
            goodsAndServiceVoList = JSON.parseArray(JSON.toJSONString(goodsInfoList), GoodsAndServiceVO.class);
        } else {
            ServiceShelf serviceShelf = new ServiceShelf();
            serviceShelf.setCsDbid(compShopMo.getCsDbid());
            serviceShelf.setSsStatus(2);
            serviceShelf.setSsApproveState(1);
            List<ServiceShelf> serviceShelfList = serviceShelfService.getListByCsDbId(serviceShelf);
            goodsAndServiceVoList = JSON.parseArray(JSON.toJSONString(serviceShelfList), GoodsAndServiceVO.class);
        }
        return goodsAndServiceVoList;
    }

    /**
     * 根据店铺类别获取所有的企业的店铺
     *
     * @param compShopMo
     * @return
     */
    @Override
    @DoPage
    public List<CompShopMO> getAllShopByType(CompShopMO compShopMo) {
        return compShopMapper.getAllShopByType(compShopMo);
    }

    /**
     * 获取所有含四新商品的店铺
     *
     * @param compShopMo
     * @return
     */
    @Override
    @DoPage
    public List<CompShopMO> getFourNewShop(CompShopMO compShopMo) {
        return compShopMapper.getFourNewShop(compShopMo);
    }

    /**
     * 根据店铺id获取店铺下所有的商品信息，接口中回传店铺id和店铺类型
     * impl中根据类型进入不同的impl方法
     * 用同一个接收体混合抛出。
     *
     * @param compShopMo
     * @return
     */
    @Override
    @DoPage
    public List<GoodsAndServiceVO> getAllGoodsByShopId(CompShopMO compShopMo) {
        List<GoodsAndServiceVO> goodsAndServiceVoList;
        if ("0".equals(compShopMo.getCsShopType())) {
            GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
            goodsInfoVO.setGoodsApproveStatus(3);
            List<GoodsInfo> goodsInfoList = goodsInfoService.getListByCsDbId(goodsInfoVO, compShopMo.getCsDbid());
            goodsAndServiceVoList = JSON.parseArray(JSON.toJSONString(goodsInfoList), GoodsAndServiceVO.class);
        } else {
            ServiceShelf serviceShelf = new ServiceShelf();
            serviceShelf.setCsDbid(compShopMo.getCsDbid());
            serviceShelf.setSsApproveState(3);
            List<ServiceShelf> serviceShelfList = serviceShelfService.getListByCsDbId(serviceShelf);
            goodsAndServiceVoList = JSON.parseArray(JSON.toJSONString(serviceShelfList), GoodsAndServiceVO.class);
        }
        return goodsAndServiceVoList;
    }

    /**
     * 根据店铺id获取店铺下的四新商品信息，接口中回传店铺id
     *
     * @param compShopMo
     * @return
     */
    @Override
    @DoPage
    public List<GoodsAndServiceVO> getFourNewGoodsByShopId(CompShopMO compShopMo) {
        List<GoodsAndServiceVO> goodsAndServiceVoList;
        FourNewGoodsVO fourNewGoodsVO = new FourNewGoodsVO();
        fourNewGoodsVO.setApproveStatus(3);
        List<FourNewGoods> fourNewGoodsInfoList = fourNewGoodsService.getByCsDbId(fourNewGoodsVO, compShopMo.getCsDbid());
        goodsAndServiceVoList = JSON.parseArray(JSON.toJSONString(fourNewGoodsInfoList), GoodsAndServiceVO.class);
        return goodsAndServiceVoList;
    }

    /**
     * 单个商品审核
     *
     * @param goodsAndServiceVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGoodsOrServiceStatus(GoodsAndServiceVO goodsAndServiceVo) {
        if (0 == goodsAndServiceVo.getType()) {
            GoodsInfo goodsInfo = new GoodsInfo();
            goodsInfo.setGoodsDbid(goodsAndServiceVo.getGoodsDbid());
            goodsInfo.setGoodsShelfState(goodsAndServiceVo.getGoodsShelfState());
            goodsInfo.setGoodsApproveStatus(goodsAndServiceVo.getGoodsApproveStatus());
            goodsInfo.setGoodsUpdateUser(UserSession.getUserId());
            goodsInfo.setGoodsUpdateTime(new Date());
            goodsInfoMapper.updateById(goodsInfo);
            //添加商品审核记录
            GoodsInfo goodsInfoRecorder = goodsInfoMapper.selectByDbId(goodsAndServiceVo.getGoodsDbid());
            GoodsInfoApproveRecorder goodsInfoApproveRecorder = new GoodsInfoApproveRecorder();
            BeanUtils.copyProperties(goodsInfoRecorder, goodsInfoApproveRecorder);
            goodsInfoApproveRecorder.setGoodsApproveRecorderId(IdGenerator.UUID());
            goodsInfoApproveRecorder.setGoodsApproveUser(UserSession.getUserId());
            goodsInfoApproveRecorder.setGoodsApproveTime(new Date());
            goodsInfoApproveRecorderMapper.insert(goodsInfoApproveRecorder);
        } else {
            ServiceShelf serviceShelf = new ServiceShelf();
            serviceShelf.setServiceDbid(goodsAndServiceVo.getServiceDbid());
            serviceShelf.setSsStatus(goodsAndServiceVo.getSsStatus());
            serviceShelf.setSsApproveState(goodsAndServiceVo.getSsApproveState());
            serviceShelfMapper.updateById(serviceShelf);
            //添加服务审核记录
            ServiceShelf serviceShelfRecorder = serviceShelfMapper.selectById(serviceShelf);
            ServiceShelfApproveRecorder serviceShelfApproveRecorder = new ServiceShelfApproveRecorder();
            BeanUtils.copyProperties(serviceShelfRecorder, serviceShelfApproveRecorder);
            serviceShelfApproveRecorder.setServiceApproveRecorderId(IdGenerator.UUID());
            serviceShelfApproveRecorder.setSsApproveUser(UserSession.getUserId());
            serviceShelfApproveRecorder.setSsApproveTime(new Date());
            serviceShelfApproveRecorderMapper.insert(serviceShelfApproveRecorder);
        }
    }

    /**
     * 单个四新商品审核
     *
     * @param goodsAndServiceVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFourNewGoodsStatus(GoodsAndServiceVO goodsAndServiceVo) {
        FourNewGoods fourNewGoods = new FourNewGoods();
        fourNewGoods.setFngDbid(goodsAndServiceVo.getFngDbid());
        fourNewGoods.setFngShelfState(goodsAndServiceVo.getFngShelfState());
        fourNewGoods.setUpdateUser(UserSession.getUserId());
        fourNewGoods.setUpdateTime(new Date());
        fourNewGoods.setApproveStatus(goodsAndServiceVo.getApproveStatus());
        fourNewGoodsMapper.updateById(fourNewGoods);
        //四新审核记录
        FourNewGoodsApproveRecorder fourNewGoodsApproveRecorder = new FourNewGoodsApproveRecorder();
        FourNewGoods fourNewGoodsRecorder = fourNewGoodsMapper.selectById(goodsAndServiceVo.getFngDbid());
        BeanUtils.copyProperties(fourNewGoodsRecorder, fourNewGoodsApproveRecorder);
        fourNewGoodsApproveRecorder.setFngApproveRecorderId(IdGenerator.UUID());
        fourNewGoodsApproveRecorder.setApproveTime(new Date());
        fourNewGoodsApproveRecorder.setApproveUser(UserSession.getUserId());
        fourNewGoodsApproveRecorderMapper.insert(fourNewGoodsApproveRecorder);
    }

    /**
     * 审核某个店铺的所有商品
     *
     * @param compShopMo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGoodsOrServiceStatusByCsDbId(CompShopMO compShopMo) {
        if ("0".equals(compShopMo.getCsShopType())) {
            GoodsInfo goodsInfo = new GoodsInfo();
            goodsInfo.setCsDbid(compShopMo.getCsDbid());
            goodsInfo.setGoodsShelfState(compShopMo.getStatus());
            goodsInfo.setGoodsApproveStatus(compShopMo.getApproveStatus());
            QueryWrapper<GoodsInfo> goodsWrapper = new QueryWrapper<>();
            goodsWrapper
                    .eq(StringUtils.isNotEmpty(goodsInfo.getCsDbid()), "CS_DBID", goodsInfo.getCsDbid())
                    .eq(goodsInfo.getGoodsShelfState() != null, "GOODS_SHELF_STATE", goodsInfo.getGoodsShelfState() == 0 ? 3 : 1)
                    .eq("GOODS_APPROVE_STATUS", 3);
            List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectList(goodsWrapper);
            goodsInfoMapper.update(goodsInfo, goodsWrapper);
            List<GoodsInfoApproveRecorder> goodsInfoApproveRecorderList = new ArrayList<>();
            for (GoodsInfo goodsInfo1 : goodsInfoList) {
                GoodsInfoApproveRecorder goodsInfoApproveRecorder = new GoodsInfoApproveRecorder();
                BeanUtils.copyProperties(goodsInfo1, goodsInfoApproveRecorder);
                goodsInfoApproveRecorderList.add(goodsInfoApproveRecorder);
            }
            goodsInfoApproveRecorderMapper.batchInsert(goodsInfoApproveRecorderList.stream().peek((object) -> {
                        object.setGoodsApproveRecorderId(IdGenerator.UUID());
                        object.setGoodsShelfState(compShopMo.getStatus());
                        object.setGoodsApproveStatus(compShopMo.getApproveStatus());
                        object.setGoodsApproveTime(new Date());
                        object.setGoodsApproveUser(UserSession.getUserId());
                    }
            ).collect(Collectors.toList()));
        } else {
            ServiceShelf serviceShelf = new ServiceShelf();
            serviceShelf.setCsDbid(compShopMo.getCsDbid());
            serviceShelf.setSsStatus(compShopMo.getStatus());
            serviceShelf.setSsApproveState(compShopMo.getApproveStatus());
            QueryWrapper<ServiceShelf> serviceWrapper = new QueryWrapper<>();
            serviceWrapper
                    .eq(StringUtils.isNotEmpty(serviceShelf.getCsDbid()), "CS_DBID", serviceShelf.getCsDbid())
                    .eq(serviceShelf.getSsStatus() != null, "SS_STATUS", serviceShelf.getSsStatus() == 0 ? 3 : 1)
                    .eq("SS_APPROVE_STATE", 3);
            List<ServiceShelf> serviceShelfList = serviceShelfMapper.selectList(serviceWrapper);
            serviceShelfMapper.update(serviceShelf, serviceWrapper);
            List<ServiceShelfApproveRecorder> serviceShelfApproveRecorderList = new ArrayList<>();
            for (ServiceShelf serviceShelf1 : serviceShelfList) {
                ServiceShelfApproveRecorder serviceShelfApproveRecorder = new ServiceShelfApproveRecorder();
                BeanUtils.copyProperties(serviceShelf1, serviceShelfApproveRecorder);
                serviceShelfApproveRecorderList.add(serviceShelfApproveRecorder);
            }
            serviceShelfApproveRecorderMapper.batchInsert(serviceShelfApproveRecorderList.stream().peek((object) -> {
                object.setServiceApproveRecorderId(IdGenerator.UUID());
                object.setSsStatus(compShopMo.getStatus());
                object.setSsApproveState(compShopMo.getApproveStatus());
                object.setSsApproveUser(UserSession.getUserId());
                object.setSsApproveTime(new Date());
            }).collect(Collectors.toList()));
        }

    }

    /**
     * 审核某个店铺的所有四新商品
     *
     * @param compShopMo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFourNewGoodsStatusByCsDbId(CompShopMO compShopMo) {
        FourNewGoods fourNewGoods = new FourNewGoods();
        fourNewGoods.setCsDbid(compShopMo.getCsDbid());
        fourNewGoods.setFngShelfState(compShopMo.getStatus());
        fourNewGoods.setApproveStatus(compShopMo.getApproveStatus());
        QueryWrapper<FourNewGoods> fourNewGoodsQueryWrapper = new QueryWrapper<>();
        fourNewGoodsQueryWrapper
                .eq(StringUtils.isNotEmpty(fourNewGoods.getCsDbid()), "CS_DBID", fourNewGoods.getCsDbid())
                .eq(fourNewGoods.getFngShelfState() != null, "FNG_SHELF_STATE", fourNewGoods.getFngShelfState() == 0 ? 3 : 1)
                .eq("APPROVE_STATUS", 3);
        List<FourNewGoods> fourNewGoodsList = fourNewGoodsMapper.selectList(fourNewGoodsQueryWrapper);
        fourNewGoodsMapper.update(fourNewGoods, fourNewGoodsQueryWrapper);
        //批量插入审核记录
        List<FourNewGoodsApproveRecorder> fourNewGoodsApproveRecorderList = new ArrayList<>();
        BeanUtils.copyProperties(fourNewGoodsList, fourNewGoodsApproveRecorderList);
        fourNewGoodsApproveRecorderMapper.batchInsert(fourNewGoodsApproveRecorderList.stream().peek((fourNewGoodsApproveRecorder) -> {
            fourNewGoodsApproveRecorder.setFngApproveRecorderId(IdGenerator.UUID());
            fourNewGoodsApproveRecorder.setFngShelfState(compShopMo.getStatus());
            fourNewGoodsApproveRecorder.setApproveStatus(compShopMo.getApproveStatus());
            fourNewGoodsApproveRecorder.setApproveTime(new Date());
            fourNewGoodsApproveRecorder.setApproveUser(UserSession.getUserId());
        }).collect(Collectors.toList()));

    }

    /**
     * 根据店铺id和其他信息搜索店铺的服务归集
     *
     * @param orderCollectionMO
     * @return
     */
    @Override
    public OrderServiceCollectionVO searchShopOrderServiceCollection(OrderCollectionVO orderCollectionMO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = "";
        String endTime = "";
        if (orderCollectionMO.getStartTime() != null) {
            startTime = simpleDateFormat.format(orderCollectionMO.getStartTime());
            endTime = simpleDateFormat.format(orderCollectionMO.getEndTime());
        }
        String type = orderCollectionMO.getType();
        OrderServiceCollectionVO orderServiceCollectionVO = new OrderServiceCollectionVO();
//        if ("2".equals(type)) {
//            orderServiceCollectionVO.setOrderServicePurchaseList(searchShopOrderPurchaseCollection(orderCollectionMO, startTime, endTime));
//        } else if ("3".equals(type)) {
//            orderServiceCollectionVO.setOrderServiceQualityList(searchShopOrderQualityCollection(orderCollectionMO, startTime, endTime));
//        } else if ("6".equals(type)) {
//            orderServiceCollectionVO.setOrderServiceSettleList(searchShopOrderSettleCollection(orderCollectionMO, startTime, endTime));
//        } else if ("5".equals(type)) {
//            orderServiceCollectionVO.setOrderServiceStorageList(searchShopOrderStorageCollection(orderCollectionMO, startTime, endTime));
//        } else if ("4".equals(type)) {
//            orderServiceCollectionVO.setOrderServiceTransportList(searchShopOrderTransportCollection(orderCollectionMO, startTime, endTime));
//        }
        return orderServiceCollectionVO;
    }

    @Override
    @DoPage
    public List<OrderServicePurchaseMO> searchShopOrderPurchaseCollection(OrderCollectionVO orderCollectionVO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "";
        String endTime = "";
        if (orderCollectionVO.getStartTime() != null) {
            startTime = simpleDateFormat.format(orderCollectionVO.getStartTime());
            endTime = simpleDateFormat.format(orderCollectionVO.getEndTime());
        }
        return orderServicePurchaseMapper.searchShopOrderPurchaseCollection(orderCollectionVO, startTime, endTime);
    }

    @Override
    @DoPage
    public List<OrderServiceQualityMO> searchShopOrderQualityCollection(OrderCollectionVO orderCollectionVO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "";
        String endTime = "";
        if (orderCollectionVO.getStartTime() != null) {
            startTime = simpleDateFormat.format(orderCollectionVO.getStartTime());
            endTime = simpleDateFormat.format(orderCollectionVO.getEndTime());
        }
        return orderServiceQualityMapper.searchShopOrderQualityCollection(orderCollectionVO, startTime, endTime);
    }

    @Override
    @DoPage
    public List<OrderServiceTransportMO> searchShopOrderTransportCollection(OrderCollectionVO orderCollectionVO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "";
        String endTime = "";
        if (orderCollectionVO.getStartTime() != null) {
            startTime = simpleDateFormat.format(orderCollectionVO.getStartTime());
            endTime = simpleDateFormat.format(orderCollectionVO.getEndTime());
        }
        return orderServiceTransportMapper.searchShopOrderTransportCollection(orderCollectionVO, startTime, endTime);
    }

    @Override
    @DoPage
    public List<OrderServiceSettleMO> searchShopOrderSettleCollection(OrderCollectionVO orderCollectionVO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "";
        String endTime = "";
        if (orderCollectionVO.getStartTime() != null) {
            startTime = simpleDateFormat.format(orderCollectionVO.getStartTime());
            endTime = simpleDateFormat.format(orderCollectionVO.getEndTime());
        }
        return orderServiceSettleMapper.searchShopOrderSettleCollection(orderCollectionVO, startTime, endTime);
    }

    @Override
    @DoPage
    public List<OrderServiceStorageMO> searchShopOrderStorageCollection(OrderCollectionVO orderCollectionVO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "";
        String endTime = "";
        if (orderCollectionVO.getStartTime() != null) {
            startTime = simpleDateFormat.format(orderCollectionVO.getStartTime());
            endTime = simpleDateFormat.format(orderCollectionVO.getEndTime());
        }
        return orderServiceStorageMapper.searchShopOrderStorageCollection(orderCollectionVO, startTime, endTime);
    }

    /**
     * 根据服务订单id获取订单下的商品信息
     *
     * @param orderCollectionMO
     * @throws IOException
     */
    @Override
    public OrderServiceGoodsVO getOrderServiceGoodsById(OrderServiceGoodsMO orderCollectionMO) {
        String orderId = orderCollectionMO.getServiceOrderId();
        String type = orderCollectionMO.getType();
        OrderServiceGoodsVO orderServiceGoodsVO = new OrderServiceGoodsVO();
        if ("2".equals(type)) {
            orderServiceGoodsVO.setOrderServicePurchaseVO(getShopOrderPurchaseGoods(orderId));
        } else if ("3".equals(type)) {
            orderServiceGoodsVO.setOrderServiceQualityVO(getShopOrderQualityGoods(orderId));
        } else if ("6".equals(type)) {
            orderServiceGoodsVO.setOrderServiceSettleVO(getShopOrderSettleGoods(orderId));
        } else if ("5".equals(type)) {
            orderServiceGoodsVO.setOrderServiceStorageVO(getShopOrderStorageGoods(orderId));
        } else if ("4".equals(type)) {
            orderServiceGoodsVO.setOrderServiceTransportVO(getShopOrderTransportGoods(orderId));
        }
        return orderServiceGoodsVO;
    }

    private OrderServicePurchaseVO getShopOrderPurchaseGoods(String orderId) {
        OrderServicePurchaseVO orderServicePurchaseVO = new OrderServicePurchaseVO();
        OrderServicePurchase orderServicePurchase = orderServicePurchaseMapper.selectById(orderId);
        List<OrderServicePurchaseGoods> orderServicePurchaseGoodsList = orderServicePurchaseGoodsMapper.getOrderServicePurchaseGoodsByOrderId(orderId);
        BeanUtils.copyProperties(orderServicePurchase, orderServicePurchaseVO);
        orderServicePurchaseGoodsList.forEach(n-> n.setOspgGoodsDbid(orderInfoMapper.getMatCodeByGoodsId(n.getOspgGoodsDbid())));
        orderServicePurchaseVO.setOrderServicePurchaseGoodsList(orderServicePurchaseGoodsList);
        return orderServicePurchaseVO;
    }

    private OrderServiceQualityVO getShopOrderQualityGoods(String orderId) {
        OrderServiceQualityVO orderServiceQualityVO = new OrderServiceQualityVO();
        OrderServiceQuality orderServiceQuality = orderServiceQualityMapper.selectById(orderId);
        List<OrderServiceQualityGoods> orderServiceQualityGoodsList = orderServiceQualityGoodsMapper.getOrderServiceQualityGoodsByOrderId(orderId);
        BeanUtils.copyProperties(orderServiceQuality, orderServiceQualityVO);
        orderServiceQualityGoodsList.forEach(n-> n.setOsqgGoodsDbid(orderInfoMapper.getMatCodeByGoodsId(n.getOsqgGoodsDbid())));
        orderServiceQualityVO.setOrderServiceQualityGoodsList(orderServiceQualityGoodsList);
        return orderServiceQualityVO;
    }

    private OrderServiceTransportVO getShopOrderTransportGoods(String orderId) {
        OrderServiceTransportVO orderServiceTransportVO = new OrderServiceTransportVO();
        OrderServiceTransport orderServiceTransport = orderServiceTransportMapper.selectById(orderId);
        List<OrderServiceTransportGoods> orderServiceTransportGoodsList = orderServiceTransportGoodsMapper.getOrderServiceTransportGoodsByOrderId(orderId);
        BeanUtils.copyProperties(orderServiceTransport, orderServiceTransportVO);
        orderServiceTransportGoodsList.forEach(n-> n.setOstgGoodsDbid(orderInfoMapper.getMatCodeByGoodsId(n.getOstgGoodsDbid())));
        orderServiceTransportVO.setOrderServiceTransportGoodsList(orderServiceTransportGoodsList);
        return orderServiceTransportVO;
    }

    private OrderServiceSettleVO getShopOrderSettleGoods(String orderId) {
        OrderServiceSettleVO orderServiceSettletVO = new OrderServiceSettleVO();
        OrderServiceSettle orderServiceSettle = orderServiceSettleMapper.selectById(orderId);
        List<OrderServiceSettleGoods> orderServiceSettleGoodsList = orderServiceSettleGoodsMapper.getOrderServiceSettleGoodsByOrderId(orderId);
        BeanUtils.copyProperties(orderServiceSettle, orderServiceSettletVO);
        orderServiceSettleGoodsList.forEach(n-> n.setOssegGoodsDbid(orderInfoMapper.getMatCodeByGoodsId(n.getOssegGoodsDbid())));
        orderServiceSettletVO.setOrderServiceSettleGoodsList(orderServiceSettleGoodsList);
        return orderServiceSettletVO;
    }

    private OrderServiceStorageVO getShopOrderStorageGoods(String orderId) {
        OrderServiceStorageVO orderServiceStorageVO = new OrderServiceStorageVO();
        OrderServiceStorage orderServiceStorage = orderServiceStorageMapper.selectById(orderId);
        List<OrderServiceStorageGoods> orderServiceStorageGoodsList = orderServiceStorageGoodsMapper.getOrderServiceStorageGoodsByOrderId(orderId);
        BeanUtils.copyProperties(orderServiceStorage, orderServiceStorageVO);
        orderServiceStorageGoodsList.forEach(n-> n.setOssgGoodsDbid(orderInfoMapper.getMatCodeByGoodsId(n.getOssgGoodsDbid())));
        orderServiceStorageVO.setOrderServiceStorageGoodsList(orderServiceStorageGoodsList);
        return orderServiceStorageVO;
    }

    /**
     * 店铺拒绝服务订单
     *
     * @param orderCollectionMO
     * @throws IOException
     */
    @Override
    public void refuseOrderService(OrderServiceGoodsMO orderCollectionMO) {
        String type = orderCollectionMO.getType();
        String orderId = orderCollectionMO.getServiceOrderId();
        if ("2".equals(type)) {
            OrderServicePurchase orderServicePurchase = orderServicePurchaseMapper.selectById(orderId);
            orderServicePurchase.setOspState("99");
            orderServicePurchaseMapper.updateById(orderServicePurchase);
        } else if ("3".equals(type)) {
            OrderServiceQuality orderServiceQuality = orderServiceQualityMapper.selectById(orderId);
            orderServiceQuality.setOsqState("99");
            orderServiceQualityMapper.updateById(orderServiceQuality);
        } else if ("6".equals(type)) {
            OrderServiceSettle orderServiceSettle = orderServiceSettleMapper.selectById(orderId);
            orderServiceSettle.setOsseState("99");
            orderServiceSettleMapper.updateById(orderServiceSettle);
        } else if ("5".equals(type)) {
            OrderServiceStorage orderServiceStorage = orderServiceStorageMapper.selectById(orderId);
            orderServiceStorage.setOssState("99");
            orderServiceStorageMapper.updateById(orderServiceStorage);
        } else if ("4".equals(type)) {
            OrderServiceTransport orderServiceTransport = orderServiceTransportMapper.selectById(orderId);
            orderServiceTransport.setOstState("99");
            orderServiceTransportMapper.updateById(orderServiceTransport);
        }
    }

    /**
     * 店铺确认服务订单
     *
     * @param orderCollectionMO
     * @throws IOException
     */
    @Override
    public void confirmOrderService(OrderServiceGoodsMO orderCollectionMO) {
        String type = orderCollectionMO.getType();
        String orderId = orderCollectionMO.getServiceOrderId();
        if ("2".equals(type)) {
            OrderServicePurchase orderServicePurchase = orderServicePurchaseMapper.selectById(orderId);
            orderServicePurchase.setOspState("2");
            orderServicePurchaseMapper.updateById(orderServicePurchase);
        } else if ("3".equals(type)) {
            OrderServiceQuality orderServiceQuality = orderServiceQualityMapper.selectById(orderId);
            orderServiceQuality.setOsqState("2");
            orderServiceQualityMapper.updateById(orderServiceQuality);
        } else if ("6".equals(type)) {
            OrderServiceSettle orderServiceSettle = orderServiceSettleMapper.selectById(orderId);
            orderServiceSettle.setOsseState("2");
            orderServiceSettleMapper.updateById(orderServiceSettle);
        } else if ("5".equals(type)) {
            OrderServiceStorage orderServiceStorage = orderServiceStorageMapper.selectById(orderId);
            orderServiceStorage.setOssState("2");
            orderServiceStorageMapper.updateById(orderServiceStorage);
        } else if ("4".equals(type)) {
            OrderServiceTransport orderServiceTransport = orderServiceTransportMapper.selectById(orderId);
            orderServiceTransport.setOstState("2");
            orderServiceTransportMapper.updateById(orderServiceTransport);
        }
    }

    /**
     * csShopName  模糊搜索店铺名称
     * csShopType  精确搜索店铺类别
     *
     * @param homeShopMo
     * @return
     */
    @Override
    @DoPage
    public List<HomeShopMO> getHomeShop(HomeShopMO homeShopMo) {
        return compShopMapper.selectHomeShop(homeShopMo, UserSession.getUserId());
    }

    /**
     * 店铺管理，获取店铺列表。
     *
     * @param compShopManageMo
     * @return
     */
    @Override
    @DoPage
    public List<CompShopManageMO> getAllCompShopManageList(CompShopManageMO compShopManageMo) {
        return compShopMapper.getAllCompShopManageList(compShopManageMo);
    }

    /**
     * 删除店铺
     *
     * @param csDbid 店铺id
     * @param flag   店铺类型：0是商品，1是服务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String csDbid, String flag) {
        //根据店铺类型判断
        if ("0".equals(flag)) {
            Integer goodsNum = goodsInfoMapper.countGoodsNum(csDbid);
            if (goodsNum > 0) {
                throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "店铺不可删除，因为有 " + goodsNum + " 件商品为已上架状态");
            } else {
                //是否有已经上架的四新商品
                Integer fourNewNum = fourNewGoodsMapper.countFourNew(csDbid);
                if (fourNewNum > 0) {
                    throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "店铺不可删除，因为有 " + fourNewNum + " 件四新商品为已上架状态");
                } else {
                    compShopMapper.deleteById(csDbid);
                    QueryWrapper<GoodsInfo> goodsInfoQueryWrapper = new QueryWrapper<>();
                    goodsInfoQueryWrapper.eq("CS_DBID", csDbid);
                    goodsInfoMapper.delete(goodsInfoQueryWrapper);
                    QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
                    orderInfoQueryWrapper.eq("ORDER_SHOP_ID", csDbid);
                    orderInfoMapper.delete(orderInfoQueryWrapper);
                }

            }
        } else {
            Integer serviceNum = serviceShelfMapper.countAlreadyServiceNum(csDbid);
            if (serviceNum > 0) {
                throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "店铺不可删除，因为有 " + serviceNum + " 个服务为已上架状态");
            } else {
                compShopMapper.deleteById(csDbid);
                QueryWrapper<ServiceShelf> serviceShelfQueryWrapper = new QueryWrapper<>();
                serviceShelfQueryWrapper.eq("CS_DBID", csDbid);
                serviceShelfMapper.delete(serviceShelfQueryWrapper);
                QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
                orderInfoQueryWrapper.eq("ORDER_SHOP_ID", csDbid);
                orderInfoMapper.delete(orderInfoQueryWrapper);
            }
        }
    }

    /**
     * 获取审核记录
     *
     * @param csDbId
     * @param flag
     * @return
     */
    @Override
    @DoPage
    public Object getApproveRecorder(String csDbId, String flag, ApproveSearchVO approveSearchVO) {
        Object object = new Object();
        if ("0".equals(flag)) {
            QueryWrapper<GoodsInfoApproveRecorder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("CS_DBID", csDbId)
                    .like(!"".equals(approveSearchVO.getName()) && approveSearchVO.getName() != null, "GOODS_NAME", approveSearchVO.getName())
                    .eq(approveSearchVO.getShelfState() != null, "GOODS_SHELF_STATE", approveSearchVO.getShelfState())
                    .eq(approveSearchVO.getApproveState() != null, "GOODS_APPROVE_STATUS", approveSearchVO.getApproveState())
                    .like(approveSearchVO.getApproveDate() != null, "GOODS_APPROVE_TIME", approveSearchVO.getApproveDate() != null ? new SimpleDateFormat("yyyy-MM-dd").format(approveSearchVO.getApproveDate()): "")
                    .orderByDesc("GOODS_APPROVE_TIME");
            object = goodsInfoApproveRecorderMapper.selectList(queryWrapper);
        }
        if ("1".equals(flag)) {
            QueryWrapper<ServiceShelfApproveRecorder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("CS_DBID", csDbId)
                    .like(!"".equals(approveSearchVO.getName()) && approveSearchVO.getName() != null, "SS_SERVICE_CLASS_NAME", approveSearchVO.getName())
                    .eq(approveSearchVO.getShelfState() != null, "SS_STATUS", approveSearchVO.getShelfState())
                    .eq(approveSearchVO.getApproveState() != null, "SS_APPROVE_STATE", approveSearchVO.getApproveState())
                    .like(approveSearchVO.getApproveDate() != null, "SS_APPROVE_TIME", approveSearchVO.getApproveDate() != null ? new SimpleDateFormat("yyyy-MM-dd").format(approveSearchVO.getApproveDate()): "")
                    .orderByDesc("SS_APPROVE_TIME");
            object = serviceShelfApproveRecorderMapper.selectList(queryWrapper);
        }
        if ("2".equals(flag)) {
            QueryWrapper<FourNewGoodsApproveRecorder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("CS_DBID", csDbId)
                    .like(!"".equals(approveSearchVO.getName()) && approveSearchVO.getName() != null, "FNG_NAME", approveSearchVO.getName())
                    .eq(approveSearchVO.getShelfState() != null, "FNG_SHELF_STATE", approveSearchVO.getShelfState())
                    .eq(approveSearchVO.getApproveState() != null, "APPROVE_STATUS", approveSearchVO.getApproveState())
                    .like(approveSearchVO.getApproveDate() != null, "APPROVE_TIME", approveSearchVO.getApproveDate() != null ? new SimpleDateFormat("yyyy-MM-dd").format(approveSearchVO.getApproveDate()): "")
                    .orderByDesc("APPROVE_TIME");
            object = fourNewGoodsApproveRecorderMapper.selectList(queryWrapper);
        }
        return object;
    }

    /**
     * 获取有审核记录的店铺列表
     *
     * @return
     */
    @Override
    @DoPage
    public List<CompShopMO> getApproveShopList(String shopTypeFlag, CompShopMO compShopMO) {
        List<String> shopIdList = compShopMapper.getShopIdList(shopTypeFlag);
        if (shopIdList.size() == 0) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "没有查询到任何审核记录");
        }
        return compShopMapper.selectByIds(shopIdList, compShopMO);
    }
}
